﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webnew.Common
{
    /// <summary>
    /// 一些常用的Javascript方法
    /// </summary>
    public static class Jscript
    {
        /// <summary>
        /// 弹出提示框
        /// </summary>
        public static void Alert(Page page, string msg)
        {
            string js = @"alert('" + msg + "')";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "Alert"))
                page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", js, true);
        }

        ///// <summary>
        ///// 列表页,编辑页 引用JS 弹出提示框
        ///// </summary>
        //public static void Alert( Page page, string msg)
        //{
        //    string js = @"alert('" + msg + "')";
        //    if (!.IsRegisterStartupScript(page.GetType(), "Alert"))
        //        page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", js, true);
        //}

        /// <summary>
        /// 弹出提示框并跳转到指定页面
        /// </summary>
        public static void AlertAndRedirect(Page page, string msg,string ToURL)
        {
            string js = @"alert('{0}');window.location.replace('{1}');";
            js = string.Format(js, msg, ToURL);
            if(!page.ClientScript.IsStartupScriptRegistered(page.GetType(),"AlertAndRedirect"))
                page.ClientScript.RegisterStartupScript(page.GetType(),"AlertAndRedirect",js,true);
        }

        /// <summary>
        /// 返回历史
        /// </summary>
        public static void GoHistory(Page page, int value)
        {
            string js = @"history.go('{0}')";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "GoHistory"))
                page.ClientScript.RegisterStartupScript(page.GetType(), "GoHistory", string.Format(js, value), true);
        }

        /// <summary>
        /// 关闭并使父窗口根据URL刷新
        /// </summary>
        public static void RefreshParent(Page page, string url)
        {
            string js = @"window.openner.location.href='{0}'; window.close();";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent"))
                page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", string.Format(js, url), true);
        }

        //// <summary> 
        /// 打开指定大小的新窗体 
        /// </summary> 
        /// <param name="url">地址</param> 
        /// <param name="width">宽</param> 
        /// <param name="heigth">高</param> 
        /// <param name="top">头位置</param> 
        /// <param name="left">左位置</param> 
        /// <param name="scoll">是否带滚动条</param>
        public static void OpenWebFowebnewize(string url, int width, int heigth, int top, int left,bool scoll, Page page) 
         {

             string js = @"window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + "," +
                          "location=no,menubar=no,resizable=no,scrollbars=" + (scoll ? "yes" : "no") + ",status=yes,titlebar=no,toolbar=no,directories=no');";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFowebnewize"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFowebnewize", js,true);
                //HttpContext.Current.Server.Transfer
            }
        }

        public static void OpenModalDialog(string ModalDialogURL, string parentwin, Page page)
        {
            string js = @"window.showModalDialog('{0}','{1}');";
            js = string.Format(js, ModalDialogURL,parentwin);
            //if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenModalDialog"))
            //    page.ClientScript.RegisterStartupScript(page.GetType(), "OpenModalDialog", js, true);
            HttpContext.Current.Response.Write("<script>" + js +"</script>");
        }
        /// <summary>
        /// 转向指定路径
        /// </summary>
        public static void TransferURL(Page page, string URL)
        {
            string js = @"window.location.href='{0}'";
            js = string.Format(js, URL);
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "TransferURL"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "TransferURL", js,true);
            }
        }

        /// <summary>
        /// 执行JS
        /// </summary>
        public static void WriteJs(Page page, string js)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "WriteJs", js, true);
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public static void CloseWindow(Page page)
        {
            string js = @"window.close()";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "CloseWindow"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "CloseWindow", js,true);
            }
        }
    }
}
